package com.gzx.spring.db.service.impl;

import com.google.common.collect.Lists;
import com.gzx.risk.core.apis.context.ApiContext;
import com.gzx.risk.core.enums.AuthStatus;
import com.gzx.risk.core.enums.RiskNodes;
import com.gzx.risk.core.pojo.*;
import com.gzx.risk.core.service.CreditService;
import com.gzx.risk.core.service.RiskService;
import com.gzx.spring.db.entity.RiskRuleResult;
import com.gzx.spring.db.service.*;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class RiskServiceImpl implements RiskService {
	@Resource
	private PhoneProductOrderService phoneProductOrderService;
	@Resource
	private PhoneUserService phoneUserService;
	@Resource
	private RiskRuleResultService riskRuleResultService;
	@Resource
	private RiskBlackService riskBlackService;
	@Resource
	private RiskWhiteService riskWhiteService;
    @Resource
    private CrossBinningService crossBinningService;
    @Resource
    private BinIntervalService binIntervalService;
    @Resource
    private UserScoreResultService userScoreResultService;
    @Resource
    private CreditService creditService;

	@Override
	public List<OrderInfo> findRiskOrders() {
		return phoneProductOrderService.getOrdersByStatus(Lists.newArrayList(2));// 待风控
	}

    @Override
    public List<OrderInfo> findRiskTwoOrders() {
        return phoneProductOrderService.getOrdersByStatus(Lists.newArrayList(99));// 待风控二阶段
    }

	@Override
	public UserInfo findUserInfo(Long uid) {
        return phoneUserService.getUser(uid);
	}

    @Override
    public UserInfo findFullUserInfo(Long uid) {
        UserInfo userInfo = this.findUserInfo(uid);
        if (Objects.isNull(userInfo)){
            return userInfo;
        }
        CreditAuthInfo effectAuthInfo = creditService.getEffectAuthInfoByUid(uid);
        if (Objects.isNull(effectAuthInfo)) {
            return userInfo;
        }
        userInfo.setCreditAuthInfo(effectAuthInfo);
        if (AuthStatus.SUBMIT.getCode().equals(effectAuthInfo.getAuthorizationStatus())) {
            userInfo.setRiskNode(RiskNodes.RISK_ONE.getCode());
        }else if (AuthStatus.AUTH_SUCCESS.getCode().equals(effectAuthInfo.getAuthorizationStatus())
                || AuthStatus.AUTH_FAILED.getCode().equals(effectAuthInfo.getAuthorizationStatus())) {
            userInfo.setRiskNode(RiskNodes.RISK_TWO.getCode());
        }
        return userInfo;
    }

    @Override
	public OrderInfo findRiskOrder(int oid) {
		return phoneProductOrderService.getOrder(oid);
	}

	@Override
	public OrderInfo findRiskOrder(String orcercode) {
		return phoneProductOrderService.getOrderInfo(orcercode);
	}
	
	@Override
	public void log(ApiContext ctx) {
		riskRuleResultService.log(ctx);
	}

	@Override
	public RiskRuleResultInfo findLastRiskRuleResult(String id) {
		return riskRuleResultService.findLastRiskRuleResult(id);
	}

	@Override
	public boolean isBlack(String id) {
		return riskBlackService.isBlack(id);
	}

	@Override
	public boolean isWhite(String id) {
		return riskWhiteService.isWhite(id);
	}
	
	
	@Override
	public boolean isInRisk(String ordercode) {
		return riskRuleResultService.getByOrdercode(ordercode) != null;
	}

    @Override
    public boolean isInRiskTwo(String ordercode) {
        List<RiskRuleResult> list = riskRuleResultService.getListByOrdercode(ordercode);
        for (RiskRuleResult riskRuleResult : list) {
            if (riskRuleResult.getRiskNode().equals(RiskNodes.RISK_TWO.getCode())) {
                return true;
            }
        }
        return false;
    }


    @Override
    public List<CrossBinningInfo> getCrossBiningInfo() {
        return crossBinningService.getCrossBiningInfo();
    }

    @Override
    public List<BinIntervalInfo> getBinIntervalInfo() {
        return binIntervalService.getBinIntervalInfo();
    }

    @Override
    public void saveUserScore(UserLevelResultInfo resultInfo){
        userScoreResultService.saveUserScoreResult(resultInfo);
    }

    @Override
    public UserLevelResultInfo getUserScoreResultByIdCard(String idCard){
        return userScoreResultService.getInfoByIdCard(idCard);
    }

    @Override
    public UserLevelResultInfo getUserScoreResultByUid(String uid) {
        return userScoreResultService.getUserScoreResultByUid(uid);
    }

}
